//
//  SEGSettingViewController.m
//  ExcellentHome
//
//  Created by 刘山 on 16/11/17.
//  Copyright © 2016年 陈区. All rights reserved.
//
#import "SEGSettingViewController.h"
#import "SEGAboutUsViewController.h"
#import "SEGSettingButtonView.h"

#import "SEGSettingViewModel.h"

#if __has_include(<ReactiveObjC/ReactiveObjC.h>)
#import <ReactiveObjC/ReactiveObjC.h>
#else
#import "ReactiveObjC.h"
#endif


@interface SEGSettingViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView* tableView;
@property(nonatomic, strong) SEGSettingViewModel *viewModel;
@end

@implementation SEGSettingViewController

#pragma mark - life cycle
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData]; //刷新开门提示音开关状态
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = self.title.length>0?self.title:@"设置";
    
    [self setupUI];
    [self segInitViews];
    [self bindViewModel];
}

- (void)setupUI
{
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = kColor_bg;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    [self.tableView setFrame:self.view.frame];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
    return self.viewModel.dataSource.count;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray* arrSon = self.viewModel.dataSource[section];
    return arrSon.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44.f;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    
    NSArray* arrSon = self.viewModel.dataSource[indexPath.section];
    
    NSDictionary* dict = arrSon[indexPath.row];
    
    if ([[dict valueForKey:@"reuseIdentifier"] isEqualToString:@"reuserIdentifier_Other"]) {
        
        SEGBaseTableViewCell * cell = [[SEGBaseTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[dict valueForKey:@"reuseIdentifier"]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        cell.textLabel.text = [dict valueForKey:@"name"];
        [cell.textLabel setFont:[UIFont systemFontOfSize:16]];
        [cell.textLabel setTextColor:kColor_1];
        
        cell.detailTextLabel.text = [[dict valueForKey:@"name"] isEqualToString:@"版本更新"] ? @"已经是最新版本" : @"";
        [cell.detailTextLabel setFont:[UIFont systemFontOfSize:13]];
        [cell.detailTextLabel setTextColor:kColor_3];
        
        cell.line.hidden = (arrSon.count - 1 <= indexPath.row) ?:NO;
        
        return cell;
    }
    
    SEGBaseTableViewCell* cell = [[SEGBaseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[dict valueForKey:@"reuseIdentifier"]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.textLabel.text = [dict valueForKey:@"name"];
    [cell.textLabel setFont:[UIFont systemFontOfSize:16]];
    [cell.textLabel setTextColor:kColor_1];
    
    UISwitch* switch_ = [[UISwitch alloc] init];
    switch_.onTintColor = kColor_T;
    cell.accessoryView = switch_;
    RACChannelTo(self.viewModel,switchStatus) = switch_.rac_newOnChannel;
    
    cell.line.hidden = (arrSon.count - 1 <= indexPath.row) ?:NO;
    
    return cell;
}

- (UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == self.viewModel.dataSource.count - 1) {
        SEGSettingButtonView* titleSectionbutton = [[SEGSettingButtonView alloc] init];
        [titleSectionbutton.titleBtn setTitle:@"退出登录" forState:UIControlStateNormal];
        UIColor *color = kColor_S1;
        [titleSectionbutton.titleBtn setBackgroundColor:color];
        titleSectionbutton.titleBtn.layer.cornerRadius = 2;
        titleSectionbutton.titleBtn.layer.masksToBounds = YES;
        titleSectionbutton.backgroundColor = kColor_bg;
        [titleSectionbutton.titleBtn addTarget:self action:@selector(logoutClick:) forControlEvents:UIControlEventTouchUpInside];
        [titleSectionbutton.titleBtn setBackgroundImage:[UIImage imageWithColor:kColor_S1] forState:UIControlStateNormal];
        [titleSectionbutton.titleBtn setBackgroundImage:[UIImage imageWithColor:kColor_S1_Press] forState:UIControlStateHighlighted];
        return titleSectionbutton;
    }
    return nil;
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSArray* arrSon = self.viewModel.dataSource[indexPath.section];
    
    NSDictionary* dict = arrSon[indexPath.row];
    
    switch ([[dict valueForKey:@"key"] integerValue]) {
        case 1: { //开门提示音
        } break;
        case 2: { //修改密码
            UIViewController *modifyPasswordView = [[SEGUhomeMediator sharedInstance]seg_initChangePasswordViewController:nil];
            [self.navigationController pushViewController:modifyPasswordView animated:YES];
            
        } break;
        case 3: { //修改手机号码
            UIViewController *modifyPhoneVC = [[SEGUhomeMediator sharedInstance]seg_initSEGChangePhoneNumberViewController:nil];
            [self.navigationController pushViewController:modifyPhoneVC animated:YES];
        } break;
        case 4: { //注销账号
            UIViewController *logoutVC = [[SEGUhomeMediator sharedInstance] seg_initSEGLogoutViewController:nil];
            [self.navigationController pushViewController:logoutVC animated:YES];
        } break;
        case 5: { //关于我们
            SEGAboutUsViewController* aboutUsView = [[SEGAboutUsViewController alloc] init];
            aboutUsView.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:aboutUsView animated:YES];
        } break;
        default:
            break;
    }
}

- (void)logoutClick:(UIButton*)sender
{
    SEGNewAlertView *alertView = [[SEGNewAlertView alloc] init];
    [alertView showWithAlertTitle:nil text:@"确定退出当前账号吗？" buttonTitlesArray:@[@"取消",@"确定"] actionBlock:^(NSInteger btnIndex) {
        if (btnIndex == 1) {
            [self.viewModel logout];
        }
    }];

}


- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section != 0) {
        return 10.f;
    }
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == self.viewModel.dataSource.count - 1) {
        return 44 + 40;
    }
    return CGFLOAT_MIN;
}



- (UITableView*)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _tableView;
}


#pragma mark - Init Views
-(void)segInitViews{}

#pragma mark - Layout
- (void)updateViewConstraints {
    [super updateViewConstraints];
}

#pragma mark - Private Methods

#pragma mark - RAC Data Binding
- (void)bindViewModel {}

#pragma mark - getter
-(SEGSettingViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[SEGSettingViewModel alloc]init];
    }
    return _viewModel;
}
#pragma mark setter
@end

